package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.system.domain.vo.DeviceListVo;
import com.ruoyi.system.mapper.AppUserMapper;
import com.ruoyi.system.service.ISysUserDeviceBindService;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDeviceMapper;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.service.ISysDeviceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysDeviceServiceImpl implements ISysDeviceService {
    private final SysDeviceMapper sysDeviceMapper;

    private final AppUserMapper appUserMapper;

    private final ISysUserDeviceBindService sysUserDeviceBindService;

    public SysDeviceServiceImpl(SysDeviceMapper sysDeviceMapper, AppUserMapper appUserMapper, ISysUserDeviceBindService sysUserDeviceBindService) {
        this.sysDeviceMapper = sysDeviceMapper;
        this.appUserMapper = appUserMapper;
        this.sysUserDeviceBindService = sysUserDeviceBindService;
    }

    /**
     * 查询设备信息
     *
     * @param deviceId 设备ID
     * @return 设备信息
     */
    @Override
    public SysDevice selectSysDeviceById(String deviceId) {
        return sysDeviceMapper.selectSysDeviceById(deviceId);
    }

    @Override
    public SysDevice selectSysDeviceByeIdentifier(String identifier) {
        return sysDeviceMapper.selectSysDeviceByeIdentifier(identifier);
    }

    @Override
    public SysDevice selectDeviceByUserId(Long userId) {
        return sysDeviceMapper.selectDeviceByUserId(userId);
    }

    /**
     * 查询设备列表
     *
     * @param sysDevice 设备信息
     * @return 设备集合
     */
    @Override
    public List<DeviceListVo> selectSysDeviceList(SysDevice sysDevice) {
        return sysDeviceMapper.selectSysDeviceList(sysDevice);
    }

    /**
     * 新增设备
     *
     * @param sysDevice 设备信息
     * @return 结果
     */
    @Override
    public int insertSysDevice(SysDevice sysDevice) {
        return sysDeviceMapper.insertSysDevice(sysDevice);
    }

    /**
     * 修改设备
     *
     * @param sysDevice 设备信息
     * @return 结果
     */
    @Override
    public int updateSysDevice(SysDevice sysDevice) {
        return sysDeviceMapper.updateSysDevice(sysDevice);
    }

    /**
     * 批量删除设备
     *
     * @param deviceIds 需要删除的设备ID
     * @return 结果
     */
    @Override
    public int deleteSysDeviceByIds(String[] deviceIds) {
        return sysDeviceMapper.deleteSysDeviceByIds(deviceIds);
    }

    /**
     * 删除设备信息
     *
     * @param deviceId 设备ID
     * @return 结果
     */
    @Override
    public int deleteSysDeviceById(String deviceId) {
        return sysDeviceMapper.deleteSysDeviceById(deviceId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindDeviceToUser(String deviceIdentifier, Long userId) {
        SysDevice device = sysDeviceMapper.selectSysDeviceByeIdentifier(deviceIdentifier);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }

        device.setDeviceStatus("1");
        sysDeviceMapper.updateSysDevice(device);

        AppUser user = appUserMapper.selectAppUserById(userId);
        user.setAreaId(device.getAreaId());
        appUserMapper.updateAppUser(user);

        sysUserDeviceBindService.recordUserDeviceBind(user.getId(), device.getDeviceId());
        return sysDeviceMapper.bindDeviceToUser(deviceIdentifier, user.getId()) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean unbindDevice(String deviceIdentifier) {
        SysDevice device = sysDeviceMapper.selectSysDeviceByeIdentifier(deviceIdentifier);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }

        AppUser user = appUserMapper.selectAppUserById(device.getBindUserId());
        user.setAreaId(null);
        appUserMapper.updateAppUser(user);

        device.setDeviceStatus("0");
        sysDeviceMapper.updateSysDevice(device);
        sysUserDeviceBindService.recordUserDeviceUnbind(device.getBindUserId(), device.getDeviceId());

        return sysDeviceMapper.unbindDevice(deviceIdentifier) > 0;
    }
}